﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace Utility
{
    public abstract class CookiesHelper
    {
        /// <summary>
        /// 根据key读取相应的cookie值
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string GetCookie(string key)
        {
            if (System.Web.HttpContext.Current.Request.Cookies[key] == null)
            {
                return string.Empty;
            }
            else
            {
                return System.Web.HttpContext.Current.Request.Cookies[key].Value;
            }
        }

        /// <summary>
        /// 向客户端写入的cookie值
        /// </summary>
        /// <param name="s"></param>
        /// <param name="strValue"></param>
        /// <param name="expires">0:关闭窗口失效 -1:立即清除 30:保存30天</param>
        public static void SetCookie(string CookieName, string strValue, int expires)
        {
            HttpContext.Current.Response.Cookies[CookieName].Value = strValue;
            HttpContext.Current.Response.Cookies[CookieName].Expires = DateTime.Now.AddDays(expires);
        }

        /// <summary>
        /// 读取Cookies
        /// </summary>
        /// <param name="strUserid"></param>
        /// <returns></returns>
        public static string GetCookie(string cookiesname, string domain)
        {
            if (Common.CheckValiable(HttpContext.Current.Request.Cookies))
            {
                if (Common.CheckValiable(HttpContext.Current.Request.Cookies[domain]))
                {
                    if (Common.CheckValiable(HttpContext.Current.Request.Cookies[domain][cookiesname]))
                    {
                        return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[domain][cookiesname].ToString());
                    }
                }
            }
            return "";
        }
        /// <summary>
        /// 目前一次只能存储一条信息
        /// </summary>
        /// <param name="CookieWord">标识</param>
        /// <param name="strValue">内容</param>
        /// <param name="expires">天数，0：页面关闭过期</param>
        public static void SetCookie(string cookiesname, string domain, string value, double expires)
        {
            HttpCookie cookie = new HttpCookie(domain);
            cookie.Values[cookiesname] = HttpContext.Current.Server.UrlEncode(value);
            if (expires > 0)
            {
                cookie.Expires = DateTime.Now.AddDays(expires);
            }
            //默认Expires 页面关闭过期
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 清除Cookies
        /// </summary>
        public static void CookieClear(string cookiesname, string domain)
        {
            HttpCookie cookie = new HttpCookie(domain);
            cookie.Values[cookiesname] = "";
            cookie.Expires = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }
}
